package com.lib.activity;

import com.andybrier.lib.R;
import com.lib.LibraryApplication;
import com.lib.activity.library.LibraryActivity;
import com.lib.activity.map.LifeIndexActivity;
import com.lib.activity.setting.SettingIndex;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TabHost;

public class IndexActivity extends TabActivity implements OnCheckedChangeListener{
	
	private TabHost mTabHost;
	private Intent infoIntent;
	private Intent libIntent;
	private Intent lifeIntent;
	private Intent setIntent;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.maintabs);
        
        LibraryApplication mApp = ((LibraryApplication) getApplicationContext());
        boolean netStatus = checkNetworkInfo();
        mApp.setNetStatus(netStatus);
        
        
        this.infoIntent = new Intent(this,InfoActivity.class);
        this.libIntent = new Intent(this,LibraryActivity.class);
        this.lifeIntent = new Intent(this,LifeIndexActivity.class);
        this.setIntent = new Intent(this,SettingIndex.class);
        
		((RadioButton) findViewById(R.id.radio_button0))
		.setOnCheckedChangeListener(this);
        ((RadioButton) findViewById(R.id.radio_button2))
		.setOnCheckedChangeListener(this);
        ((RadioButton) findViewById(R.id.radio_button3))
		.setOnCheckedChangeListener(this);
        ((RadioButton) findViewById(R.id.radio_button4))
		.setOnCheckedChangeListener(this);
        
        setupIntent();
    }

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked){
			switch (buttonView.getId()) {
			case R.id.radio_button0:
				this.mTabHost.setCurrentTabByTag("A_TAB");
				break;
			case R.id.radio_button2:
				this.mTabHost.setCurrentTabByTag("LIB_TAB");
				break;
			case R.id.radio_button3:
				this.mTabHost.setCurrentTabByTag("LIFE_TAB");
				break;
			case R.id.radio_button4:
				this.mTabHost.setCurrentTabByTag("MORE_TAB");
				break;
			}
		}
		
	}
	
	private void setupIntent() {
		this.mTabHost = getTabHost();
		TabHost localTabHost = this.mTabHost;

		localTabHost.addTab(buildTabSpec("A_TAB", R.string.main_tab_info,
				R.drawable.icon_1_n, this.infoIntent));

		localTabHost.addTab(buildTabSpec("LIB_TAB",
				R.string.main_tab_lib, R.drawable.icon_3_n,
				this.libIntent));

		localTabHost.addTab(buildTabSpec("LIFE_TAB", R.string.main_tab_life,
				R.drawable.icon_4_n, this.lifeIntent));

		localTabHost.addTab(buildTabSpec("MORE_TAB", R.string.main_tab_set,
				R.drawable.icon_5_n, this.setIntent));

	}
	
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel),
				getResources().getDrawable(resIcon)).setContent(content);
	}
	
	private boolean checkNetworkInfo() {
      boolean netStatus = false;

      ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo networkInfo = connMgr
              .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      boolean isWifiConn = networkInfo.isConnected();
      networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
      boolean isMobileConn = false;
      if (networkInfo != null)
          isMobileConn = networkInfo.isConnected();
      Log.i("WIFI", "Wifi connected: " + isWifiConn);
      Log.i("Mobile", "Mobile connected: " + isMobileConn);

      if (isWifiConn || isMobileConn)
          netStatus = true;
      return netStatus;
  }
}